﻿using System;
using System.Collections.Generic;
using System.Drawing;

using NGE;

namespace MH.MENU
{
    public class Shop : Panelbox
    {
        public Shop(Game g)
            : base(g)
        {
            Name = "商店";
            //Location = new Microsoft.DirectX.Vector3();
            //Size = new System.Drawing.Size(320, 240);
            //BGColor = Color.FromArgb(150, 0, 0, 255);


            //OnPanelboxOpen += new MenuEvent(OnInit);
            desp = new Textbox();
            desp.Text = "";
            desp.Location = new Microsoft.DirectX.Vector3(160, 20, 0);
            Elements.Add(desp);

            pagelabe = new Textbox();
            pagelabe.Text = "1/1";
            pagelabe.Location = new Microsoft.DirectX.Vector3(0, 200, 0);
            Elements.Add(pagelabe);

            page_index = 0;

            OnPanelboxOpen += new MenuEvent(Buy_OnPanelboxOpen);
            OnPanelboxRefresh += new MenuEvent(Buy_OnPanelboxRefresh);
        }

        void Buy_OnPanelboxRefresh(object sender, object arg)
        {
            update_content();
        }

        void Buy_OnPanelboxOpen(object sender, object arg)
        {
            if (arg == null)
            {
                return;
            }
            props = sceneCOM.GetCHR((string)arg).Bag;
            page_count = props.Count / page_max_count;
            if (props.Count % page_max_count > 0)
            {
                page_count++;
            }

            page_index = 1;
            update_content();
        }

        SceneComponent sceneCOM
        {
            get
            {
                return ((SceneComponent)game.GetComponent("SceneCOM"));
            }
        }
        ScriptComponent scriptCOM
        {
            get
            {
                return ((ScriptComponent)game.GetComponent("ScriptCOM"));
            }
        }

        Textbox desp;
        Textbox pagelabe;

        List<Prop> props;
        int page_index;
        int page_count;
        int page_max_count = 8;


        void update_content()
        {
            scriptCOM.SetValue("menu_return", "null");

            Buttonboxes.Clear();

            pagelabe.Text = " " + page_index + "/" + page_count + "  钱包：" + sceneCOM.PC.G + "G";
            for (int i = 0; i < page_max_count; i++)
            {
                try
                {
                    Prop prop = props[(page_index - 1) * page_max_count + i];
                    Buttonbox bb = new Buttonbox();
                    bb.Textbox.Text = prop.Name;
                    bb.Location = new Microsoft.DirectX.Vector3(20, 20 + i * 20, 0);
                    bb.OnButtonboxEvent += new MenuEvent(bb_OnButtonboxEvent);
                    Buttonboxes.Add(bb);
                }
                catch
                {
                    break;
                }
            }
            //if (page_index != page_index_bak)
            {
                ResetButtonboxIndex();
            }
            //else
            {
                
            }
        }



        int prop_base_index
        {
            get
            {
                return (page_index - 1) * page_max_count;
            }
        }
        void bb_OnButtonboxEvent(object sender, object arg)
        {
            Buttonbox me = (Buttonbox)sender;
            int i = me.StopIndex;
            if ((ButtonboxState)arg == ButtonboxState.Up)
            {
                string prop_name = props[prop_base_index + i].Name;
                int prop_worth = props[prop_base_index + i].Worth;
                //scriptCOM.SetValue("prop_to_buy", prop_name);
                scriptCOM.AS("menu open 是否 确定要买" + prop_name + "吗？/n价格：" + prop_worth + "G");
                scriptCOM.AS("block menu_return # null");

                string cmd = "";
                cmd += "if menu_return = yes\n";
                cmd += "then if " + sceneCOM.PC.G + " >= " + prop_worth + "\n";
                cmd += "then #pc_name g- " + prop_worth + "\n";
                cmd += "then #pc_name bagin " + prop_name + "\n";
                cmd += "then menu refresh 商店";
                scriptCOM.AS(cmd);

                //object obj = scriptCOM.GetValue("menu_return");
                //if (obj != null)
                //{
                //    string str = (string)obj;
                //    if (str == "yes")
                //    {
                //        //string prop_name = (string)scriptCOM.GetValue("prop_to_buy");
                //        scriptCOM.IS();

                //    }
                //}
            }
            else if ((ButtonboxState)arg == ButtonboxState.Focus)
            {
                desp.Text = "价格：\n" + props[prop_base_index + i].Worth + "G\n\n" + props[prop_base_index + i].Description;
            }
        }

        protected override void OnKeyboardEventCode(object sender, int key, KeyboardState ks)
        {
            if (ks == KeyboardState.KeyDown)
            {
                //page_index_bak = page_index;
                if (key == game.Option.KeyL)
                {
                    page_index--;
                    if (page_index < 1)
                    {
                        page_index = 1;
                    }
                    update_content();
                }
                else if (key == game.Option.KeyR)
                {
                    page_index++;
                    if (page_index > page_count)
                    {
                        page_index = page_count;
                    }
                    update_content();
                }
            }
            base.OnKeyboardEventCode(sender, key, ks);
        }
    }
}
